/*
  SimpleMQTTClient.ino
  The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
  Once it connects successfully to a Wifi network and a MQTT broker, it subscribe to a topic and send a message to it.
  It will also send a message delayed 5 seconds later.
*/

#include "EspMQTTClient.h"
#include <Adafruit_NeoPixel.h>
EspMQTTClient client(
  "HUAWEI-354",
  "88888888",
  "120.77.16.8",  // MQTT Broker server ip
  "deng",   // Can be omitted if not needed
  "jialei",   // Can be omitted if not needed
  "TestClient",     // Client name that uniquely identify your device
  61613              // The MQTT port, default to 1883. this line can be omitted
);
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
 
// 控制 WS2812 灯条的引脚编号
#define PIN        D2
 
//定义控制的 LED 数量
#define NUMPIXELS 40
 
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
//相邻 LED 之间的延迟，单位毫秒
#define DELAYVAL 500
void setup()
{
  Serial.begin(115200);
  #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
  #endif
  // END of Trinket-specific code.
 
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)

  // Optionnal functionnalities of EspMQTTClient : 
  client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
  client.enableLastWillMessage("TestClient/lastwill", "I am going offline");  // You can activate the retain flag by setting the third parameter to true
}

// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
  // Subscribe to "mytopic/test" and display received message to Serial
  client.subscribe("deng/led", [](const String & payload) {
    Serial.println(payload);

      //int receivedChar =payload.toInt();
      //String receivedChar = payload;
      //receivedChar.trim();
      //Serial.println(receivedChar);
      int LampSwitch_l = payload.indexOf('"');
      int LampSwitch_r = payload.indexOf('"',LampSwitch_l+1);

      int LampSwitch_value_l = payload.indexOf('"',LampSwitch_r+1);
      int LampSwitch_value_r = payload.indexOf('"',LampSwitch_value_l+1);

      String LampSwitch_value=payload.substring(LampSwitch_value_l+1,LampSwitch_value_r);
      Serial.println(LampSwitch_value);
      
      int LED_R_l = payload.indexOf('"',LampSwitch_value_r+1);
      int LED_R_r = payload.indexOf('"',LED_R_l+1);

      int LED_R_value_l = payload.indexOf('"',LED_R_r+1);
      int LED_R_value_r = payload.indexOf('"',LED_R_value_l+1);

      String LED_R_value=payload.substring(LED_R_value_l+1,LED_R_value_r);
      Serial.println(LED_R_value);
      
      int LED_G_l = payload.indexOf('"',LED_R_value_r+1);
      int LED_G_r = payload.indexOf('"',LED_G_l+1);
            
      int LED_G_value_l = payload.indexOf('"',LED_G_r+1);
      int LED_G_value_r = payload.indexOf('"',LED_G_value_l+1);

      String LED_G_value=payload.substring(LED_G_value_l+1,LED_G_value_r);
      Serial.println(LED_G_value);
      
      int LED_B_l = payload.indexOf('"',LED_G_value_r+1);
      int LED_B_r = payload.indexOf('"',LED_B_l+1);

      int LED_B_value_l = payload.indexOf('"',LED_B_r+1);
      int LED_B_value_r = payload.indexOf('"',LED_B_value_l+1);

      String LED_B_value=payload.substring(LED_B_value_l+1,LED_B_value_r);
      Serial.println(LED_B_value);

      int Led_id = LampSwitch_value.toInt();
      int LED_R_value_int = LED_R_value.toInt();
      int LED_G_value_int = LED_G_value.toInt();
      int LED_B_value_int = LED_B_value.toInt(); 
      Serial.println("  "+(String)LED_R_value_int+"  "+(String)LED_G_value_int+"  "+(String)LED_B_value_int);                             
      /*
    { "LampSwitch":1,  3,14,
    "LED_R":0,         24,30
    "LED_G":151,       40,46
    "LED_B":254}       
    */
    if(Led_id==2016){
      pixels.clear(); 
      for(int i=0; i<NUMPIXELS; i++) 
           pixels.setPixelColor(i, pixels.Color(LED_R_value_int, LED_G_value_int, LED_B_value_int));
      }
    
     pixels.show();
       
    
  });

  // Publish a message to "mytopic/test"
  client.publish("deng/led", "This is a message"); // You can activate the retain flag by setting the third parameter to true

  // Execute delayed instructions
  client.executeDelayed(5 * 1000, []() {
    client.publish("deng/led", "This is a message sent 5 seconds later");
  });
}

void loop()
{
  client.loop();
}
